/*
 *      texturemanager.cpp
 *      
 *      Copyright 2009 Víctor Manuel Ochoa <kanito8a@gmail.com>
 *      
 *      This program is free software; you can redistribute it and/or modify
 *      it under the terms of the GNU General Public License as published by
 *      the Free Software Foundation; either version 2 of the License, or
 *      (at your option) any later version.
 *      
 *      This program is distributed in the hope that it will be useful,
 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *      GNU General Public License for more details.
 *      
 *      You should have received a copy of the GNU General Public License
 *      along with this program; if not, write to the Free Software
 *      Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *      MA 02110-1301, USA.
 */


#include "texturemanager.h"


/*
 * 
 * name: desconocido
 * @param
 * @return
 */
TextureManager::TextureManager()
{
	
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
TextureManager::~TextureManager()
{
	
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
bool TextureManager::init()
{
  	m_pLog=&Log::getInstance();
	m_game_textures.clear();
	
	m_pLog->debug("+ Texture Manager");
	return true;	
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
void TextureManager::freeMemory()
{
	list<Texture *>::iterator i;
	for(i=m_game_textures.begin();i!=m_game_textures.end();i++){
		(*i)->freeMemory();
		delete (*i);
	}
	m_game_textures.clear();
	m_pLog->debug("- Texture Manager");
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Texture *TextureManager::getTexture(const char *_name)
{
	list<Texture *>::iterator i;
	i=m_game_textures.begin();
	
	Texture *result=0;
	
	while(result==0 && i!=m_game_textures.end())
	{
		if(!strcmp((*i)->getFileName(),_name))
			result=(*i);

		i++;
	}

	return result;
}

/*
 * 
 * name: desconocido
 * @param
 * @return
 */
Texture *TextureManager::newTexture(const char *_name)
{
	if(!_name)
	{
		return 0;
	}
	
	Texture *result=getTexture(_name);
	if(!result)
	{
		result=new Texture();
		
		if(result->init(_name))
		{
			m_game_textures.push_back(result);
		}
		else
		{
			result->freeMemory();
			result=0;
			m_pLog->error("newTexture");
		}

	}
	
	m_pLog->debug("+ Texture manager -> New Texture");
	return result;
}
